<?php
class Fpt_Business_Subpage_Adapter_MySql extends Fpt_Business_Subpage_Adapter_Abstract
{
    /**
     * Instance
     */
    protected static $_instance = null;

    /**
     * Constructor of class
     */
    public function __construct(){}

    /**
     * Get singletom instance
     */
    public final static function getInstance()
    {
        //Check instance
        if ( is_null(self::$_instance) )
        {
            self::$_instance    = new self();
        }//end if

        //Return instance
        return self::$_instance;
    }

    /**
     * Insert article
     */
    public function insertSubpage($params)
    {
        try
        {
            //Get db
            $subpageMaster = Fpt_Global::getSubpageMasterDB();
            $stmt   = $subpageMaster->prepare('CALL sp_suppage_insertMatchExtend(:p_matchid, :p_imageteam1, :p_imageteam2, :p_matchinfo, :p_matchhistory, :p_matchsquad, :p_leagueperformance, :p_playerstar, :p_rightperfomance, :p_leftsquad, :p_rightsquad, :p_leftsuspend, :p_rightsusped, :p_goalteam1, :p_goalteam2, :p_infoteam1, :p_infoteam2, :p_matchcode, :p_imagebackground, :p_title, :p_description, :p_keyword, :p_facebooklink, :p_colorteam1, :p_colorteam2);');
            $stmt->bindParam('p_matchid', $params['match_id'], PDO::PARAM_INT);
            $stmt->bindParam('p_imageteam1', $params['image_team1'], PDO::PARAM_STR);
            $stmt->bindParam('p_imageteam2', $params['image_team2'], PDO::PARAM_STR);
            $stmt->bindParam('p_matchinfo', $params['match_info'], PDO::PARAM_STR);
            $stmt->bindParam('p_matchhistory', $params['match_history'], PDO::PARAM_STR);
            $stmt->bindParam('p_matchsquad', $params['match_squad'], PDO::PARAM_STR);
            $stmt->bindParam('p_leagueperformance', $params['left_performance'], PDO::PARAM_STR);
            $stmt->bindParam('p_playerstar', $params['player_star'], PDO::PARAM_STR);
            $stmt->bindParam('p_rightperfomance', $params['right_performance'], PDO::PARAM_STR);
            $stmt->bindParam('p_leftsquad', $params['left_squad'], PDO::PARAM_STR);
            $stmt->bindParam('p_rightsquad', $params['right_squad'], PDO::PARAM_STR);
            $stmt->bindParam('p_leftsuspend', $params['left_suspend'], PDO::PARAM_STR);
            $stmt->bindParam('p_rightsusped', $params['right_suspend'], PDO::PARAM_STR);
            $stmt->bindParam('p_goalteam1', $params['goal_team1'], PDO::PARAM_INT);
            $stmt->bindParam('p_goalteam2', $params['goal_team2'], PDO::PARAM_INT);
            $stmt->bindParam('p_infoteam1', $params['info_team1'], PDO::PARAM_STR);
            $stmt->bindParam('p_infoteam2', $params['info_team2'], PDO::PARAM_STR);
            $stmt->bindParam('p_matchcode', $params['match_code'], PDO::PARAM_STR);
            $stmt->bindParam('p_imagebackground', $params['image_background'], PDO::PARAM_STR);
            $stmt->bindParam('p_title', $params['title'], PDO::PARAM_STR);
            $stmt->bindParam('p_description', $params['description'], PDO::PARAM_STR);
            $stmt->bindParam('p_keyword', $params['keyword'], PDO::PARAM_STR);
            $stmt->bindParam('p_facebooklink', $params['facebook_link'], PDO::PARAM_STR);
            $stmt->bindParam('p_colorteam1', $params['color_team1'], PDO::PARAM_STR);
            $stmt->bindParam('p_colorteam2', $params['color_team2'], PDO::PARAM_STR);
            $stmt->execute();
            
            
            //Select output
            $result = $stmt->fetch();
            
            //Closed cursor
            $stmt->closeCursor();

            //Unset
            unset($stmt);
            unset($outputObj);
            //Return
            return $result['result'];
        }
        catch ( Exception $e )
        {
            Subpage::logError($e);
            return 0;
        }//end try catch
        
        //Return
        return 0;
    }
    
    /**
     * Get article detail
     */
    public function getDetailMatchExtend($matchid)
    {       
        try
        {
            //Get db
            $subpageSlave   = Fpt_Global::getSubpageSlaveDB();
            $stmt           = $subpageSlave->prepare('CALL sp_subpage_getDetailMatchExtend(:p_matchid);');
            $stmt->bindParam('p_matchid', $matchid, PDO::PARAM_INT);
            $stmt->execute();
            $result = $stmt->fetch();

            //Closed cursor
            $stmt->closeCursor();

            //Unset
            unset($stmt);

            //Return
            return $result;
        }
        catch ( Exception $e )
        {
            Subpage::logError($e);
            return array();
        }//end try catch
    }
    
    /*
     * update article
     * @param <array> $params
     * return <int> $result
     */
    public function updateMatchExtend($params)
    {
        try
        {
            $subpageMaster = Fpt_Global::getSubpageMasterDB();
            $stmt   = $subpageMaster->prepare('CALL sp_suppage_updateMatchExtend(:p_matchid, :p_imageteam1, :p_imageteam2, :p_matchinfo, :p_matchhistory, :p_matchsquad, :p_leagueperformance, :p_playerstar, :p_rightperfomance, :p_leftsquad, :p_rightsquad, :p_leftsuspend, :p_rightsusped, :p_goalteam1, :p_goalteam2, :p_infoteam1, :p_infoteam2, :p_matchcode, :p_imagebackground, :p_title, :p_description, :p_keyword, :p_facebooklink, :p_colorteam1, :p_colorteam2);');
            $stmt->bindParam('p_matchid', $params['match_id'], PDO::PARAM_INT);
            $stmt->bindParam('p_imageteam1', $params['image_team1'], PDO::PARAM_STR);
            $stmt->bindParam('p_imageteam2', $params['image_team2'], PDO::PARAM_STR);
            $stmt->bindParam('p_matchinfo', $params['match_info'], PDO::PARAM_STR);
            $stmt->bindParam('p_matchhistory', $params['match_history'], PDO::PARAM_STR);
            $stmt->bindParam('p_matchsquad', $params['match_squad'], PDO::PARAM_STR);
            $stmt->bindParam('p_leagueperformance', $params['left_performance'], PDO::PARAM_STR);
            $stmt->bindParam('p_playerstar', $params['player_star'], PDO::PARAM_STR);
            $stmt->bindParam('p_rightperfomance', $params['right_performance'], PDO::PARAM_STR);
            $stmt->bindParam('p_leftsquad', $params['left_squad'], PDO::PARAM_STR);
            $stmt->bindParam('p_rightsquad', $params['right_squad'], PDO::PARAM_STR);
            $stmt->bindParam('p_leftsuspend', $params['left_suspend'], PDO::PARAM_STR);
            $stmt->bindParam('p_rightsusped', $params['right_suspend'], PDO::PARAM_STR);
            $stmt->bindParam('p_goalteam1', $params['goal_team1'], PDO::PARAM_INT);
            $stmt->bindParam('p_goalteam2', $params['goal_team2'], PDO::PARAM_INT);
            $stmt->bindParam('p_infoteam1', $params['info_team1'], PDO::PARAM_STR);
            $stmt->bindParam('p_infoteam2', $params['info_team2'], PDO::PARAM_STR);
            $stmt->bindParam('p_matchcode', $params['match_code'], PDO::PARAM_STR);
            $stmt->bindParam('p_imagebackground', $params['image_background'], PDO::PARAM_STR);
            $stmt->bindParam('p_title', $params['title'], PDO::PARAM_STR);
            $stmt->bindParam('p_description', $params['description'], PDO::PARAM_STR);
            $stmt->bindParam('p_keyword', $params['keyword'], PDO::PARAM_STR);
            $stmt->bindParam('p_facebooklink', $params['facebook_link'], PDO::PARAM_STR);
            $stmt->bindParam('p_colorteam1', $params['color_team1'], PDO::PARAM_STR);
            $stmt->bindParam('p_colorteam2', $params['color_team2'], PDO::PARAM_STR);
            $stmt->execute();

            //Select output            
            $result     = $stmt->fetch();

            //Closed cursor
            $stmt->closeCursor();            

            //Unset
            unset($stmt);

            //Return
            return (isset($result['result']) && $result['result']==1)?true:false;
        }
        catch ( Exception $e )
        {
            Subpage::logError($e);
            return false;
        }//end try catch        
    }
    
     /*
     * get List Match_Extend
     * @param <array> $para, <int> $limit, <int> $offset
     * return <array> $result
     */
    public function getListMatchExtend($condition,$limit,$offset, $orderby)
    {
        try
        {
            //Get db
            $SubpageMaster = Fpt_Global::getSubpageSlaveDB();

            $stmt       = $SubpageMaster->prepare('CALL sp_subpage_getListMatchExtend(:p_matchid, :p_fromdate, :p_todate, :p_orderby, :p_limit, :p_offset, @p_rowcount);');
            $stmt->bindParam('p_matchid', $condition['match_id'], PDO::PARAM_INT);
            $stmt->bindParam('p_fromdate', $condition['from_time'], PDO::PARAM_INT);
            $stmt->bindParam('p_todate', $condition['to_time'], PDO::PARAM_INT);
            $stmt->bindParam('p_orderby', $orderby, PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $limit, PDO::PARAM_INT);
            $stmt->bindParam('p_offset', $offset, PDO::PARAM_INT);
            $stmt->execute();
            $result = $stmt->fetchAll();

            //Closed cursor
            $stmt->closeCursor();
            //Fetch data
            $stmt = $SubpageMaster->query("SELECT @p_rowcount");
            $total = $stmt->fetchColumn();
            $response = array('rows'=>$result, 'total'=>$total);
        }
         catch (Exception $e)
         {
            Subpage::logError($e);
            $response = array('rows'=>array(), 'total'=>0);
         }
         return $response;
    }
    
    /**
     * delete Subpage
     */
    public function deleteSubpage($matchId)
    {
        try
        {            
            //Get db
            $subpageMaster = Fpt_Global::getSubpageMasterDB();
            $stmt       = $subpageMaster->prepare('CALL  sp_subpage_deleteMatchExtend(:p_matchid);');
            $stmt->bindParam('p_matchid', $matchId, PDO::PARAM_INT);
            $stmt->execute();
            $result = $stmt->fetch();

            //Closed cursor
            $stmt->closeCursor();

            //Unset
            unset($stmt);

            //Return
            return (isset($result['result']) && $result['result']==1)?true:false;
        }
        catch ( Exception $e )
        {
            Subpage::logError($e);
            return false;
        }//end try catch
    }
    
    /**
     * Get list comment
     */
    public function getListCommentByMatch($params)
    {
        try
        {
            //Get db
            $subpageSlave   = Fpt_Global::getSubpageSlaveDB();
            $stmt           = $subpageSlave->prepare('CALL sp_subpage_getCommentByMatch(:p_matchid, :p_teamid, :p_isapprove);');
            $stmt->bindParam('p_matchid', $params['match_id'], PDO::PARAM_INT);
            $stmt->bindParam('p_teamid', $params['team_id'], PDO::PARAM_INT);
            $stmt->bindParam('p_isapprove', $params['is_approve'], PDO::PARAM_STR);
            $stmt->execute();
            $result = $stmt->fetchAll();

            //Closed cursor
            $stmt->closeCursor();

            //Unset
            unset($stmt);

            //Return
            return $result;
        }
        catch ( Exception $e )
        {
            Subpage::logError($e);
            return array();
        }//end try catch
    }
    
    /**
     * Get list comment
     */
    public function getListComment($params)
    {
        try
        {
            //Get db
            $subpageSlave   = Fpt_Global::getSubpageSlaveDB();
            $stmt           = $subpageSlave->prepare('CALL sp_subpage_getListComments(:p_isapprove, :p_limit, :p_offset, @p_rowcount);');
            $stmt->bindParam('p_isapprove', $params['is_approve'], PDO::PARAM_STR);
            $stmt->bindParam('p_limit', $params['limit'], PDO::PARAM_INT);
            $stmt->bindParam('p_offset', $params['offset'], PDO::PARAM_INT);
            $stmt->execute();
            $result = $stmt->fetchAll();

            //Closed cursor
            $stmt->closeCursor();

            //Select output
            $outputObj  = $subpageSlave->query('SELECT @p_rowcount AS total');
            $arrTotal   = $outputObj->fetch();

            //Closed cursor
            $outputObj->closeCursor();

            //Unset
            unset($stmt);
            unset($outputObj);

            //Return
            return array('list'=>$result, 'total'=>$arrTotal['total']);
        }
        catch ( Exception $e )
        {
            Subpage::logError($e);
            return array('list'=>null, 'total'=>0);
        }//end try catch
    }
    
    /**
     * delete comment
     */
    public function deleteComment($commentId)
    {
        try
        {
            //Get db
            $subpageMaster = Fpt_Global::getSubpageMasterDB();
            $stmt       = $subpageMaster->prepare('CALL sp_subpage_deleteComments(:p_commentid);');
            $stmt->bindParam('p_commentid', $commentId, PDO::PARAM_INT);
            $stmt->execute();
            $result = $stmt->fetch();

            //Closed cursor
            $stmt->closeCursor();

            //Unset
            unset($stmt);

            //Return
            return (isset($result['result']) && $result['result']==1)?true:false;
        }
        catch ( Exception $e )
        {
            Subpage::logError($e);
            return false;
        }//end try catch
    }
    
    /**
     * update comment
     */
    public function updateComment($params)
    {
        try
        {
            //Get db
            $subpageMaster = Fpt_Global::getSubpageMasterDB();
            $stmt       = $subpageMaster->prepare('CALL sp_subpage_updatecomments(:p_commentid, :p_status, :p_content, :p_isapprove);');
            $stmt->bindParam('p_commentid', $params['comment_id'], PDO::PARAM_INT);
            $stmt->bindParam('p_status', $params['status'], PDO::PARAM_INT);
            $stmt->bindParam('p_content', $params['content'], PDO::PARAM_STR);
            $stmt->bindParam('p_isapprove', $params['is_approve'], PDO::PARAM_INT);
            $stmt->execute();
            $result = $stmt->fetch();

            //Closed cursor
            $stmt->closeCursor();

            //Unset
            unset($stmt);

            //Return
            return (isset($result['result']) && $result['result']==1)?true:false;
        }
        catch ( Exception $e )
        {
            Subpage::logError($e);
            return false;
        }//end try catch
    }
    
    /**
     * Get detail comment
     */
    public function getDetailComment($comment_id)
    {
        try
        {
            //Get db
            $subpageSlave   = Fpt_Global::getSubpageSlaveDB();
            $stmt           = $subpageSlave->prepare('CALL sp_subpage_getDetailComment(:p_commentid);');
            $stmt->bindParam('p_commentid', $comment_id, PDO::PARAM_INT);
            $stmt->execute();
            $result = $stmt->fetch();

            //Closed cursor
            $stmt->closeCursor();

            //Unset
            unset($stmt);

            //Return
            return $result;
        }
        catch ( Exception $e )
        {
            Subpage::logError($e);
            return array();
        }//end try catch
    }

}

